﻿using System.Collections;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
using Caliburn.Micro;
using ThunderboltCustomControlLibrary;
using System.Windows.Controls;
using System.Windows.Documents;
using HandyControl.Controls;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using HandyControl.Data;
using ThunderboltInterfaceSharp.Views.UserControls;
using System.Threading.Tasks;
using MessageBox = System.Windows.MessageBox;

namespace ThunderboltInterfaceSharp.ViewModels.UserControls;

public class TopLevelSidebarViewModel:Screen
{
	public delegate void SwitchForm(FormTitle title);
	public event SwitchForm SwitchFormEvent;
	public string Title { get; set; }

	public bool isPV { get; set; }
	public ObservableCollection<IconPathValueButton> ItemCollection { get; set; } = new ObservableCollection<IconPathValueButton>();
	public IconPathValueButton SelectedItem { get; set; }
	//public System.Action SwitchFormEvent;
	public bool IsShowConnectPhone { get; set; }
	public bool IsMouseInConnectPhone { get; set; }

	public  Visibility CarouselPictureVisibility { get; set; }
	public Visibility PictureVisibility { get; set; }

	public TopLevelSidebarViewModel()
	{

		isPV = true;
		Task.Run(() =>
		{
			while (true)
			{
				foreach (var btn in ItemCollection)
				{
					btn.IsPanelVisible = !btn.IsPanelVisible;
					isPV = !isPV;
					Thread.Sleep(100);
				}
			}
		});
		//todo:在构造函数里定义控件会报错，待查明
		//var rou = new RoundButton();
		//var btn = new IconPathValueButton(Orientation.Horizontal);
		//{
		// Content = "下载",

		// VerticalContentAlignment = System.Windows.VerticalAlignment.Center,
		// HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center,
		// FontSize = 13,Width = 57,
		// //IconMargin = new Thickness(8),
		// Foreground = Brushes.DarkGray,
		// Orientation = Orientation.Horizontal,
		// IconSize = 22,
		// Height = 40
		//};
		//var g = //Path里面有Geometry属性，Path是Geometry图像的载体
		//	Geometry.Parse(geometryCollection
		//		.FirstOrDefault(s => s.Key.ToString().Equals("FilmVaultGeometryGroup")).Value.ToString());
		//ItemCollection = new ObservableCollection<IconPathValueButton>()
		//{

		//};

		//SelectedItem = ItemCollection.FirstOrDefault();
	}


	protected override void OnViewLoaded(object view)
	{
		base.OnViewLoaded(view);

		var resource = new ResourceManager("ThunderboltInterfaceSharp.Language.StringResource", Assembly.GetExecutingAssembly());
		List<(string,GeometryGroup)> aList = new List<(string,GeometryGroup)> ();
		var icon = new GeometryGroup();

		var dic = new ResourceDictionary { Source = new Uri("././Resources/Styles/GeometryStyle.xaml", UriKind.Relative) };
		foreach (DictionaryEntry it in dic)
		{
			var key = it.Key.ToString();
			var value = it.Value;
			icon = value as GeometryGroup;
			aList.Add( (key,icon));
			//value.Bounds
		}
		var dicArray = new DictionaryEntry[100];
		dic.CopyTo(dicArray, 0);
		var geometryCollection = dicArray.ToList();
		var id = Thread.CurrentThread.ManagedThreadId;
		ItemCollection = new ObservableCollection<IconPathValueButton>()
		{
			 new IconPathValueButton()
			{
			Content = resource.GetObject("Download",Thread.CurrentThread.CurrentCulture),
			BorderThickness = new Thickness(0),
			VerticalContentAlignment = System.Windows.VerticalAlignment.Center,
			HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center,
			FontSize = 13,Width = 57,
			IsPanelVisible = false,
			Cursor =Cursors.Hand,
			Icon =  (aList
				.FirstOrDefault(s => s.Item1.ToString().Equals("DownloadGeometry")).Item2),
		IconMargin = new Thickness(5),
			Foreground = Brushes.DarkGray,
			Orientation = Orientation.Vertical,
			IconSize = 22,
			Height = 70
			},
			 new IconPathValueButton()
			 {
				 Content = resource.GetObject("CloudDisk",Thread.CurrentThread.CurrentCulture),
				 VerticalContentAlignment = System.Windows.VerticalAlignment.Center,
				 HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center,
				 FontSize = 13,Width = 57,
				 IsPanelVisible = false,
				 Cursor =Cursors.Hand,

				 Icon =  (aList
					 .FirstOrDefault(s => s.Item1.ToString().Equals("CloudGeometryGroup")).Item2),
				 IconMargin = new Thickness(5),
				 Foreground = Brushes.DarkGray,
				 Orientation = Orientation.Vertical,
				 IconSize = 22,
				 Height = 70
			 },
			 new IconPathValueButton()
			 {
				 Content = resource.GetObject("Games",Thread.CurrentThread.CurrentCulture),
				 IsPanelVisible = false,
				 Cursor =Cursors.Hand,

				 VerticalContentAlignment = System.Windows.VerticalAlignment.Center,
				 HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center,
				 FontSize = 13,Width = 57,
				 Icon =  (aList
					 .FirstOrDefault(s => s.Item1.ToString().Equals("GameGeometryGroup")).Item2),
				 IconMargin = new Thickness(5),
				 Foreground = Brushes.DarkGray,
				 Orientation = Orientation.Vertical,
				 IconSize = 22,
				 Height = 70
			 },
			 new IconPathValueButton()
			 {
				 IsPanelVisible = false,
				 Content = resource.GetObject("Members",Thread.CurrentThread.CurrentCulture),
				 Cursor =Cursors.Hand,

				 VerticalContentAlignment = System.Windows.VerticalAlignment.Center,
				 HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center,
				 FontSize = 13,Width = 57,
				 Icon =  (aList
					 .FirstOrDefault(s => s.Item1.ToString().Equals("MemberGeometryGroup")).Item2),
				 IconMargin = new Thickness(5),
				 Foreground = Brushes.DarkGray,
				 Orientation = Orientation.Vertical,
				 IconSize = 22,
				 Height = 70
			 },
			 new IconPathValueButton()
			 {
				 Content = resource.GetObject("More",Thread.CurrentThread.CurrentCulture),
				 IsPanelVisible = false,
				 Cursor =Cursors.Hand,

				 VerticalContentAlignment = System.Windows.VerticalAlignment.Center,
				 HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center,
				 FontSize = 13,Width = 57,
				 Icon =  (aList
					 .FirstOrDefault(s => s.Item1.ToString().Equals("MoreGeometryGroup")).Item2),
				 IconMargin = new Thickness(5,5,5,5),
				 Foreground = Brushes.DarkGray,
				 Orientation = Orientation.Vertical,
				 IconSize = 22,
				 Height = 70
			 }
		};
		var Ss = (aList
			.FirstOrDefault(s => s.Item1.ToString().Equals("FilmVaultGeometryGroup")).Item2);
		//var s = (geometryCollection
		//	.FirstOrDefault(s => s.Key.ToString().Equals("FilmVaultGeometryGroup")).Value.ToString());
		//var g = //Path里面有Geometry属性，Path是Geometry图像的载体
		//	Geometry.Parse(geometryCollection
		//		.FirstOrDefault(s => s.Key.ToString().Equals("FilmVaultGeometryGroup")).Value.ToString());
		SelectedItem = ItemCollection.FirstOrDefault();
		SelectedItem.Foreground = Brushes.DodgerBlue;
		foreach (var btn in ItemCollection)
		{
			btn.Click += ItemSelectedChanged;
		}

		
	}
	//cm:Message.Attach="[Event SelectionChanged ]=[Action ItemSelectedChanged($this,$eventArgs)];[Event PreviewMouseLeftButtonDown ]=[Action lbxMouseLeftButtonDown($this,$eventArgs)]"
	//todo:事件逻辑待整理
	public void ItemSelectedChanged(object sender, EventArgs e)
	{
		try
		{
			SelectedItem = sender as IconPathValueButton;
			SelectedItem.Foreground = Brushes.DodgerBlue;
			ItemCollection.Where(s => s.Content != SelectedItem.Content).ToList()
				.ForEach(s => { s.Foreground = Brushes.DarkGray; });
				OnSwitchForm((FormTitle)ItemCollection.IndexOf(SelectedItem));
		}
		catch (Exception ex)
		{
			throw new Exception("error:", ex);
		}
		
	}

	public void OnSwitchForm(FormTitle title)
	{
		SwitchFormEvent?.Invoke(title);
	}
	public void lbxMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
	{
		//todo:点到button上不会触发，暂时舍弃
		SelectedItem = sender as IconPathValueButton;
	}

	public void ConnectPhoneMouseEnter()
	{
		IsShowConnectPhone = true;
	}
	public void ConnectPhoneMouseLeave(MouseEventArgs e)
	{
		Task.Run(() =>
		{
			Thread.Sleep(200);
			if (IsMouseInConnectPhone) return;
			//var location = e.GetPosition(_topFunctionBar.LoginGuidePopup);
			IsShowConnectPhone = false;
		});

	}
	public void ConnectPhonePopupMouseEnter()
	{
		IsMouseInConnectPhone = true;
	}
	public async void ConnectPhonePopupMouseLeave(MouseEventArgs e)
	{

		IsMouseInConnectPhone = false;
		await Task.Run(() =>
		{
			Thread.Sleep(200);
		});
		IsShowConnectPhone = false;
	}

	public void BtnComputerHoverA()
	{
		CarouselPictureVisibility = Visibility.Visible;
		PictureVisibility = Visibility.Collapsed;
	}
	public void BtnComputerHoverB()
	{
		CarouselPictureVisibility = Visibility.Collapsed;
		PictureVisibility = Visibility.Visible;
	}

	public void BtnPhoneHover()
	{

	}
}
public enum FormTitle : byte
{
	Download = 0,
	CloudDisk,
	Games,
	Members,
	More
}